﻿<cfsilent>
	<cfscript>
		
		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");
		sequenceProgramPlan = getProperty("serviceFactory").getBean("sequenceProgramPlan");
		
		response = structNew();
		structInsert(response, "success", false, true);
		
		academicYear = event.getArg("AcademicYear");
		academicTerm = event.getArg("AcademicTerm");
		subjectId = event.getArg("SubjectID");

		
		if ( len(academicYear) and len(academicTerm) and len(subjectId) ) {

			transaction {
				
				sql = "SELECT term_id 
						FROM t_term 
						WHERE academic_year = :academicYear 
							  AND 
							  academic_term = :academicTerm " ;
							  
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="academicYear", value=academicYear, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="academicTerm", value=academicTerm, cfsqltype="cf_sql_char" );
				
				rs_term = queryObj.execute( sql=sql ).getResult();
			
				
				/* 学期代码 */
				termId = rs_term.term_id;
								
				sql = "SELECT 
							sbj_id, 
							sbj_name, 
							sbj_direction 
						FROM 
							t_subject 
						WHERE 
							sbj_id = :subjectId " ;
				
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="subjectId", value=subjectId, cfsqltype="cf_sql_varchar" );
				
				rs_subject = queryObj.execute( sql=sql ).getResult();
				

				if ( len(subjectId) eq 5 ) {
					/* 此时为专业 */
					subjectClass = rs_subject.sbj_id; /* 行政班所属专业代码 */
					subjectDir = "0";  /* 学生专业方向 */
				}
				else {
					/* 此时为方向 */
					subjectClass = left(rs_subject.sbj_id, 5);  /* 行政班所属专业代码 */
					subjectDir = rs_subject.sbj_direction;  /* 学生专业方向 */
				}
				
				sql = "SELECT  
							grade, cid, prop_id, cna_id 
						FROM 
							t_schema 
						WHERE 
							academic_year = :academicYear 
							AND 
							academic_term = :academicTerm 
							AND 
							sbj_id = :subjectId 
						ORDER BY 
							grade, cid "; 
							
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="academicYear", value=academicYear, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="academicTerm", value=academicTerm, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="subjectId", value=subjectId, cfsqltype="cf_sql_varchar" );
				
				rs_schema = queryObj.execute( sql=sql ).getResult();
	
				for ( a=1; a LTE rs_schema.recordCount; a++ ){
			
					courseId = rs_schema["cid"][a]; /* 课程代码 */
					propId =  rs_schema["prop_id"][a]; /* 课程属性 必修/选修 */
					courseNature = rs_schema["cna_id"][a]; /* 课程性质 公共/专业/实验 */
					grade = rs_schema["grade"][a]; /* 年级 */
					
					sql = "SELECT cls_id FROM t_class 
							WHERE 
								substr(grade,1,4) = :grade 
								AND 
								sbj_id = :subjectClass 
							ORDER BY cls_id "; 
					
					queryObj = new Query( datasource=application.dnsMaster );
					
					queryObj.addParam( name="grade", value=trim(grade), cfsqltype="cf_sql_char" );
					queryObj.addParam( name="subjectClass", value=subjectClass, cfsqltype="cf_sql_varchar" );
					
					rs_class = queryObj.execute( sql=sql ).getResult();
					
					for ( b=1; b LTE rs_class.recordCount; b++ ){
						
						classId = rs_class["cls_id"][b]; /* 课程代码 */
						
						/* 生成教学计划主键 */
						sequenceProgramPlan.setClassID( classId );
						sequenceProgramPlan.setCourseID( courseId );	
						
						planId = sequenceProgramPlan.getID();
						
						
						/* 检查教学计划是否存在 */
						sql = "SELECT sbj_direction FROM t_plan WHERE p_id = :planId "; 
						
						queryObj = new query( datasource=application.dnsMaster );
						queryObj.addParam( name="planId", value=planId, cfsqltype="cf_sql_varchar" );

						rs_check = queryObj.execute( sql=sql ).getResult();
						
						
						if ( rs_check.recordCount EQ 0 ) { 
													
							sql = "INSERT INTO t_plan 
										(
											p_id, cid, cls_id, cidx, prop_id, cna_id, term_id, sbj_direction 
										)
										VALUES(
											:planId, :courseId, :classId, :courseIndex, :propId, :courseNature, :termId, :sbjDirection 										
										)";
												  
							queryObj = new query( datasource=application.dnsMaster );
							
							queryObj.addParam( name="planId", value=planId, cfsqltype="cf_sql_varchar" );
							queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" );
							queryObj.addParam( name="classId", value=classId, cfsqltype="cf_sql_char" );
							queryObj.addParam( name="courseIndex", value="000", cfsqltype="cf_sql_char" );
							queryObj.addParam( name="propId", value=propId, cfsqltype="cf_sql_char" );
							queryObj.addParam( name="courseNature", value=courseNature, cfsqltype="cf_sql_char" );
							queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
							queryObj.addParam( name="sbjDirection", value=subjectDir, cfsqltype="cf_sql_varchar" );
							
							queryObj.execute( sql=sql ); 
							
						} 
						else {                                                                                                                                                                                         

							/* 修复班级课程的专业方向 如果培养方案的不同方向开设相同课程 */
							savedDir = rs_check.sbj_direction;
							
							if ( not listFind( savedDir, subjectDir, "/" ) ) {
								savedDir = listAppend( savedDir, subjectDir, "/" );
							}

							sql = "UPDATE t_plan 	
										SET
											cid = :courseId,
											cls_id = :classId,
											prop_id = :propId, 
											cna_id = :courseNature,
											term_id = :termId,
											sbj_direction = :sbjDirection 
										WHERE 
											p_id = :planId ";
										  
							queryObj = new query( datasource=application.dnsMaster );
							
							queryObj.addParam( name="planId", value=planId, cfsqltype="cf_sql_varchar" );
							queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" );
							queryObj.addParam( name="classId", value=classId, cfsqltype="cf_sql_char" );
							queryObj.addParam( name="propId", value=propId, cfsqltype="cf_sql_char" );
							queryObj.addParam( name="courseNature", value=courseNature, cfsqltype="cf_sql_char" );
							queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
							queryObj.addParam( name="sbjDirection", value=savedDir, cfsqltype="cf_sql_varchar" );
							
							queryObj.execute( sql=sql ); 
							
						}
				
					}

				}
				
				tarSubject = structNew();
				
				structInsert(tarSubject, "SubjectName", rs_subject.sbj_name, true);
				structInsert(tarSubject, "SubjectID", rs_subject.sbj_id, true);
				
				structInsert(response, "subject", tarSubject, true);
				
				structInsert(response, "success", true, true);
			}
		}
		
	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data:response, stringNumbers:true)#</cfoutput>